package com.example.vv.model.account.base.presenter;

import com.example.vv.fragmentWork.entity.ChaptersEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.fragmentWork.utlis.LogUtils;
import com.example.vv.model.account.base.contract.AccountContract;
import com.example.vv.model.account.base.model.AccountModel;
import com.example.vv.res.net.BaseResponse;
import com.example.vv.res.net.NetUrl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class AccountPresenter extends MvpPresenter<AccountContract.View,AccountContract.Model> implements AccountContract.Presenter{
    List<String> title = new ArrayList<>();


    @Override
    public void attach(AccountContract.View view) {
        super.attach(view);
        model = new AccountModel();
    }

    @Override
    public void setChapter() {
        model.GET_CHAPTER(NetUrl.GET_API_CHAPTER)
                .map(new Function<BaseResponse, List<ChaptersEntity>>() {
                    @Override
                    public List<ChaptersEntity> apply(BaseResponse baseResponse) {


                        return new Gson().fromJson(baseResponse.getData(),new TypeToken<List<ChaptersEntity>>(){}.getType());
                    }
                })
                .subscribe(new Consumer<List<ChaptersEntity>>() {
                    @Override
                    public void accept(List<ChaptersEntity> chaptersEntities) throws Exception {
                        view().getChapter(chaptersEntities);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtils.Log(throwable.getMessage()+"");
                    }
                });
    }

    @Override
    public List<String> setTitle(List<ChaptersEntity> chaptersEntities) {
        for (int i = 0; i < chaptersEntities.size(); i++) {
            title.add(chaptersEntities.get(i).getName());
        }
        return title;
    }
}
